package net.wangds.iocomp.utils;

import net.wangds.log.helper.LogHelper;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StreamUtils {

    public static String readAsString(InputStream is,String encoding){
        if(is==null){
            return null;
        }
        String res = "";
        try(ByteArrayOutputStream bos=new ByteArrayOutputStream()){
            byte[] buf = new byte[5120];
            int len;
            while((len = is.read(buf))>=0){
                bos.write(buf,0,len);
            }
            bos.flush();
            res = new String(buf, encoding);
        } catch (IOException e) {
            LogHelper.error(e.getLocalizedMessage(),e);
        }
        return res;
    }

}
